<?php
class system_controller_action_helper_admin extends system_controller_action_helper_abstract {
	
	public function init() {
		
	}
	
	public function direct($args = array()) {
		if(!($user = system_user_instance::getRow())) {
			$this->_exitAndShow($args);
			return true;
		}
		/**
		 * 安全登录验证
		 */
		$session = system_user_instance::getInstance()->getSession();
		if($session->getSecurity() && $user->getSecure() != $session->getSecurity()) {
			$session->logout();
			$this->_exitAndShow($args, 'We found that your account has been logined in another location, and you have been forced to logout!');
			return true;
		}
		return false;
	}
	
	private function _exitAndShow($args = array(), $exception = null, $action = 'login') {
		if(isset($args[0]) && isset($args[0][0]) && isset($args[0][0]['ajax'])) {
			header('Content-type:text/json;charset=utf8');
			if($action == 'login') {
				$exception = 'Login time out.Please relogin again.';
			}
			echo json_encode(array('status' => 0, 'message' => $exception));
			exit;
		}
		$controller = $this->getActionController();
		$request = $controller->getRequest();
		$controller->view->exception = $exception;
		$controller->view->referer = $this->getFrontController()->getInstance()
									->getRouter()
									->assemble($request->getControllerName() . '/' . $request->getActionName(), $request->getParams());
		$request->setControllerName('index')->setActionName($action)->setDispatched(false);
	}
}
